﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="YUITree.aspx.cs" Inherits="ZX.WebApp.EditWidows.YUITree" %>

<%@ Register Assembly="ZX.Web.UI.Controls" Namespace="ZX.Web.UI.Controls" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <!--CSS file (default YUI Sam Skin) -->
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/reset-fonts-grids/reset-fonts-grids.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/container/assets/skins/sam/container.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/menu/assets/skins/sam/menu.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/button/assets/skins/sam/button.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/resize/assets/skins/sam/resize.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/layout/assets/skins/sam/layout.css" />
    <link type="text/css" rel="stylesheet" href="../Scripts/yui_2.9.0/build/datatable/assets/skins/sam/datatable.css" />
    <link href="../Scripts/yui_2.9.0/build/treeview/assets/treeview-core.css" rel="stylesheet"
        type="text/css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/fonts/fonts-min.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/treeview/assets/skins/sam/treeview.css" />
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/element/element-min.js"></script>
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/datasource/datasource-min.js"></script>
    <!-- OPTIONAL: JSON Utility (for DataSource) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/json/json-min.js"></script>
    <!-- OPTIONAL: Connection Manager (enables XHR for DataSource) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/connection/connection-min.js"></script>
    <!-- OPTIONAL: Get Utility (enables dynamic script nodes for DataSource) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/get/get-min.js"></script>
    <!-- OPTIONAL: Drag Drop (enables resizeable or reorderable columns) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/dragdrop/dragdrop-min.js"></script>
    <!-- OPTIONAL: Calendar (enables calendar editors) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/calendar/calendar-min.js"></script>
    <!-- Source files -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/datatable/datatable-min.js"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/layout/layout-min.js"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/button/button-min.js"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/container/container-min.js"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/treeview/treeview-min.js"></script>
    <script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
</head>
<body class="yui-skin-sam">
    <form id="form1" runat="server">
    
    <!-- 工具栏 -->
    <div id="listpage"> 

        <cc1:TreeView ID="TreeView1" runat="server"></cc1:TreeView>
       
    </div>
        
    </form> 
       <form id="form2" action="../../Extend/AjaxData/">
              <textarea style="display: none" id="jsonDataContent" rows="2" cols="2" name="jsonDataContent"></textarea>
         </form>
</body>
<script type="text/javascript">
    var tree2 = null;
    (function () {
        var Dom = YAHOO.util.Dom,
        Event = YAHOO.util.Event;
        Event.onDOMReady(function () {
            var layout = new YAHOO.widget.Layout({
                units: [
                    { position: 'center', body: 'TreeView1', gutter: '1 2 1 1', header: '列表', scroll: true}]
            });
            layout.render();
        });

        var makeBranch = function (parent, label) {
        }

        tree2 = new YAHOO.widget.TreeView("TreeView1");
        makeBranch(tree2.getRoot());
        //debugger;
        tree2.setNodesProperty('propagateHighlightUp', <%= this.GetStringFromQueryString("IsHighlightUpDown", "true")%>);
        tree2.setNodesProperty('propagateHighlightDown', <%= this.GetStringFromQueryString("IsHighlightUpDown", "true")%>);
        
        tree2.subscribe('clickEvent', tree2.onEventToggleHighlight);
        tree2.render();
        if('<%= this.GetStringFromQueryString("IsExpandAll", "false")%>'=='true')
        {
            tree2.expandAll();
        }
        //取得角色的权限设置 
        var request = "../../Extend/AjaxData/DataProcessin.aspx?actioin=Get&entity=SysRoleFunction&condition=RoleID='" + parent.pId + "'";
        YAHOO.util.Connect.asyncRequest('POST', request, {
            success: function (res) {
                var inventory;
                try {
                    var roleIDs = [];
                    var str = res.responseText;
                    var items = eval("[" + str + "]");
                    for (var i = 0; i < items[0].rows.length; i++) {
                        roleIDs.push(items[0].rows[i].FunctionID);
                    }
                    var childNodes = tree2.getNodeCount();
                    var topNodes = tree2.getNodesByProperty("href");
                    for (var i = 0; i < childNodes; ++i) {
                        var id = topNodes[i].href.split("#@")[1];
                        if (roleIDs.join(",").indexOf(id) > -1) {
                            //for (var node in topNodes) {
                            topNodes[i].highlightState = 1; // setNodesByProperty('highlightState');
                        }
                    }
                    tree2.render();
                }
                catch (e) {
                }
                finally {
                }
            },
            failure: function (error) {
                alert("读取菜单数据错误：" + error.statusText);
            }
        }, null);

    })();
    function checkNode(userIDs) {
        //debugger;
        var topNodes = tree2.getRoot().children;
        for (var i = 0; i < topNodes.length; ++i) {
            var id = topNodes[i].href.split("#@")[1];
            if (userIDs.indexOf(id) > -1) {
                topNodes[i].highlightState = 1; // setNodesByProperty('highlightState');
            }    nn  
        }
    }
    var ids="";
    var fkey;
    var names = "";
    var flag = "";
    function Native(caption, key) {
        flag = "single";//标识为单选 
        fkey = key;
        if (ids.indexOf(fkey) == -1) {
            ids = fkey + ",";
            names = caption + ",";
        }
    }

    //设置已选节点
    function checkSelectedNode(ids) {
        tree2.render();
        //debugger;
        var inventory;
        var arrids=ids.split(",");
        try {
            var childNodes = tree2.getNodeCount();
            var topNodes = tree2.getNodesByProperty("href");
            for (var i = 0; i < childNodes; ++i) {
                var id = topNodes[i].href.split("#@")[1];
//                if (ids.indexOf(id) > -1) {
//                    topNodes[i].highlightState = 1; 
//                }
                for(var j=0;j<arrids.length;j++)
                {
                   if (arrids[j] ==id) {
                        topNodes[i].highlightState = 1; 
                    }
                }
            }
            tree2.render();
        }
        catch (e) {
        }
        finally {
        }

        //setTimeout(setcheckSelectedNode(ids), 200);
    }

    //空，只是为了等待树加载完
    function setcheckSelectedNode(ids){
        var inventory;
        var arrids=ids.split(",");
        try {
            var childNodes = tree2.getNodeCount();
            var topNodes = tree2.getNodesByProperty("href");
            for (var i = 0; i < childNodes; ++i) {
                var id = topNodes[i].href.split("#@")[1];
//                if (ids.indexOf(id) > -1) {
//                    topNodes[i].highlightState = 1; 
//                }
                for(var j=0;j<arrids.length;j++)
                {
                   if (arrids[j] ==id) {
                        topNodes[i].highlightState = 1; 
                    }
                }
            }
            tree2.render();
        }
        catch (e) {
        }
        finally {
        }
    }

    //得到选中的值
    function ChooseInfo() {
        
        if (flag != "single") {
            var hiLit = tree2.getNodesByProperty('highlightState', 1);
            var hiLit1 = null;
            if ('<%=MasterEntityName %>' == "SysRoleFunction")
            {
                 hiLit1=tree2.getNodesByProperty('highlightState', 2);
             }
            var labels = [];
            if (YAHOO.lang.isNull(hiLit)) {
                parent._fn("", "");
                return;
                //alert("没有选择任何项!");
                //return;
            } else {
                for (var i = 0; i < hiLit.length; i++) {
                    var node = tree2.getTreeDefinition();
                    labels.push(hiLit[i].href.split("#@")[1]);
                    names = names + hiLit[i].label;
                    names = names + ",";
                }
                if (!YAHOO.lang.isNull(hiLit1)) {
                    for (var i = 0; i < hiLit1.length; i++) {
                        var node = tree2.getTreeDefinition();
                        labels.push(hiLit1[i].href.split("#@")[1]);
                    }
                }
            }
            parent._fn("", "");
            //ids = ids.substring(0, ids.length - 1);
            names = names.substring(0, names.length - 1);
            parent._fn(labels, names);
        }
        else {
            parent._fn("", "");
            ids = ids.substring(0, ids.length - 1);
            names = names.substring(0, names.length - 1);
            parent._fn(ids, names);
        }

    }
    function Save() {
    
        var hiLit = tree2.getNodesByProperty('highlightState', 1);
        var hiLit1 = tree2.getNodesByProperty('highlightState', 2);
        var labels = [];
            if (!YAHOO.lang.isNull(hiLit)) {
                for (var i = 0; i < hiLit.length; i++) {
                    var node = tree2.getTreeDefinition();
                    labels.push(hiLit[i].href.split("#@")[1]);
                }
            }
            if (!YAHOO.lang.isNull(hiLit1)) {
                for (var i = 0; i < hiLit1.length; i++) {
                    var node = tree2.getTreeDefinition();
                    labels.push(hiLit1[i].href.split("#@")[1]);
                }
            }
            document.getElementById("jsonDataContent").value = labels;
            var formObject = document.getElementById('form2');
            YAHOO.util.Connect.setForm(formObject);
            var url = "../../Extend/AjaxData/DataProcessin.aspx?action=Save&entity=<%=MasterEntityName %>&roleid=" + parent.pId + "";
            YAHOO.util.Connect.asyncRequest('POST', url, {
                success: function (res) {
                    var inventory;
                    try {
                        inventory = JSON.parse(res.responseText);
                        if (inventory == 1) {
                            alert("保存成功!");
                            parent.YAHOO.example.container.dialog1.hide();
                        } else {
                            alert("保存失败!");
                        }
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("读取菜单数据错误：" + error.statusText);
                }
            }, null);
    }
</script>
</html>
